package com.situ.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.common.util.PageResult;
import com.situ.mall.mapper.CollectMapper;
import com.situ.mall.pojo.entity.Collect;
import com.situ.mall.pojo.query.CollectQuery;
import com.situ.mall.pojo.vo.CollectVO;
import com.situ.mall.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Override
    public void addCollect(Collect collect) {
        collectMapper.insertSelective(collect);
    }

    @Override
    public void cancelCollect(Collect collect) {
        collectMapper.cancelCollect(collect);
    }

    @Override
    public Collect checkCollect(Collect collect) {
        return collectMapper.checkCollect(collect);
    }

    @Override
    public PageResult collectList(CollectQuery collectQuery) {
        //使用PageHelper开启分页
        PageHelper.startPage(collectQuery.getPage(), collectQuery.getLimit());
        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
        List<CollectVO> list = collectMapper.collectList(collectQuery);
        //查找总数量PageHelper会自动帮我们完成
        //int totalCount = userMapper.selectTotalCount(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount, list);
    }

    @Override
    public void deleteCollect(Integer id) {
        collectMapper.deleteByPrimaryKey(id);
    }
}
